<template>
	<form class="ha-form-group" @submit="formSubmit">
		<view class="uni-form-item uni-column">
			<view class="title">团体名称</view>
			<view class="item">
				<input
					placeholder-style="font-size: 14px;"
					type="text"
					name="name"
					placeholder="如四（3）班、软件2组、财务部、xx公司、xx学校等"
					:value="group_item.name"
				/>
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">标签</view>
			<view class="item">
				<input
					placeholder-style="font-size: 14px;"
					type="text"
					name="label"
					placeholder="如学校、班级、公司、部门等"
					:value="group_item.label"
				/>
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">是否开放成员注册（默认开放）</view>
			<view class="item">
				<switch :class="group_item.is_reg?'checked':''" :checked="group_item.is_reg" name="is_reg" @change="regChange" />
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">是否分开使用（是否断开本团体的上级和下级的关系，公司、学校等使用）</view>
			<view class="item">
				<switch :class="group_item.is_split?'checked':''" :checked="group_item.is_split" name="is_split" @change="splitChange" />
			</view>
		</view>
		<view class="uni-btn-v">
			<button :loading="false" form-type="submit">提交修改</button>
		</view>
	</form>
</template>

<script>
	export default {
		data() {
			return {
				group_id: '',
				group_item: {}
			}
		},
		onLoad() {
			this.group_id = this.$route.query.group_id;
			if (this.group_id) {
				this.getOneGroup();
			} else {
				uni.showToast({
					icon: 'none',
					title: '出错了，请确认页面来源！',
					duration: 1000
				});
			}
		},
		methods: {
			regChange: function(e) {
				this.group_item.is_reg = e.target.value;
			},
			splitChange: function(e) {
				this.group_item.is_split = e.target.value;
			},
			getOneGroup() {
				uniCloud.callFunction({
					name: "ha_get_group",
					data: {
						type: 2,
						group_id: this.group_id
					},
					success: ({result}) => {
						// console.log(result);

						if(result.status === 0){
							this.group_item = result.data;
						}else{
							uni.showToast({
								icon: 'none',
								title: `${result.msg || "网络异常，请稍后重试！"}`,
								duration: 1000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '网络异常，请稍后重试！'
						});
					}
				});
			},
			formSubmit(e) {
				// console.log(e.detail.value);
				let {
					name
				} = e.detail.value;

				name = name.replace(/\s*/g,"");
				if ( name.length < 3 ) {
					uni.showModal({
						content: '名称长度不能小于3',
						showCancel: false
					})
					return
				}
				e.detail.value.name = name;

				uniCloud.callFunction({
					name: "ha_update_group",
					data: Object.assign(e.detail.value, {
						type: 3,
						group_id: this.group_id
					}),
					success: ({result}) => {
						// console.log(result);

						if ( result.status === 0 ) {
							uni.showToast({
								title: `${result.msg || "更新成功！"}`,
								duration: 1000
							});
							setTimeout(() => {
								uni.redirectTo({
									url: "/pages/index/index"
								});
							}, 1000);
						} else {
							uni.showToast({
								icon: 'none',
								title: `${result.msg || "网络异常，请稍后重试！"}`,
								duration: 1000
							});
						}
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '网络异常，请稍后重试！'
						});
					}
				});
			}
		}
	}
</script>

<style>

</style>
